import roleStore from '@/store/role-play.js';
import store from '@/store/index.js';
// 白名单 不需要token
const whiteList = [
	'/pages/pages_network/login/wx_login',
	'/pages_user/login/login',
	'/pages/pages_user/login/login',
	'/pages_user/login/forget',
	'/pages_user/login/register',
	'/pages_user/login/wxphone',
	'/pages/pages_network/login/login',
	'/pages_network/login/forget',
	'/pages_network/login/register',
	'/pages_network/login/wxphone',
	'/pages_network/service_agreement/service_agreement',
	'/pages_network/privacy_policy/privacy_policy',
];
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'];
function hasPermission(url) {
	// 在白名单中或有token，直接跳转
	if (
		whiteList.indexOf(url) !== -1 ||
		uni.getStorageSync('access-user').token
	) {
		return true;
	}
	return false;
}
function hasPermission_net(url) {
	// 在白名单中或有token，直接跳转
	if (
		whiteList.indexOf(url) !== -1 ||
		uni.getStorageSync('access-network').token
	) {
		return true;
	}
	return false;
}
const routeInterceptor = async () => {
	list.forEach((item) => {
		uni.addInterceptor(item, {
			// 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
			invoke(e) {
				const url = e.url.split('?')[0];
				if (url.includes('pages_user')) {
					if (
						!(
							store.state.userMobileHasLogin ||
							uni.getStorageSync('access-user').token
						)
					)
						return true;
				}
				if (hasPermission_net(url)) return;
				if (uni.getStorageSync('role_play') == 'u-user') {
					// 获取要跳转的页面路径（url去掉"?"和"?"后的参数）
					const datetime = new Date().getTime(); //当前时间戳
					const dataValue = uni.getStorageSync('access-user');
					const token = dataValue.token; //获取token值
					const logintime = dataValue.logintime; //获取时登录时间戳
					const timeToken = dataValue.timeToken; //过期时间(秒)
					if (token) {
						if ((datetime - logintime) / 1000 > timeToken) {
							uni
								.showModal({
									title: '提示',
									content: '登录过期，请重新登录！',
									showCancel: false,
								})
								.then((res) => {
									//确认
									if (res.confirm) {
										//关闭所有页面，打开到应用内的某个页面
										uni.navigateTo({
											url:
												'/pages/pages_user/login/login?custCode=' +
												uni.getStorageSync('custCode'),
										});
										uni.setStorageSync('role_play', 'u-user');
										//关闭所有页面，打开到应用内的某个页面
										uni.removeStorageSync('access-user');
									}
								});
							return false;
						}
					}
					// 将用户的目标路径保存下来
					// 这样可以实现 用户登录之后，直接跳转到目标页面
					// uni.setStorageSync("URL", url)
					if (!hasPermission(url)) {
						uni.navigateTo({
							url:
								'/pages/pages_user/login/login?custCode=' +
								uni.getStorageSync('custCode'),
						});
						return false;
					}
				} else if (uni.getStorageSync('role_play') == 'u-network') {
					// 获取要跳转的页面路径（url去掉"?"和"?"后的参数）

					const datetime = new Date().getTime(); //当前时间戳
					const dataValue = uni.getStorageSync('access-network');
					const token = dataValue.token; //获取token值
					const logintime = dataValue.logintime; //获取时登录时间戳
					const timeToken = dataValue.timeToken; //过期时间(秒)
					if (token) {
						if ((datetime - logintime) / 1000 > timeToken) {
							uni
								.showModal({
									title: '提示',
									content: '登录过期，请重新登录！',
									showCancel: false,
								})
								.then((res) => {
									//确认
									if (res.confirm) {
										//关闭所有页面，打开到应用内的某个页面
										uni.reLaunch({
											url: '/pages/pages_network/login/login',
										});
										uni.setStorageSync('role_play', 'u-network');
										//关闭所有页面，打开到应用内的某个页面
										uni.removeStorageSync('access-network');
									}
								});
							return false;
						}
					}
					// 将用户的目标路径保存下来
					// 这样可以实现 用户登录之后，直接跳转到目标页面
					// uni.setStorageSync("URL", url)
					if (!hasPermission_net(url)) {
						uni.reLaunch({
							url: '/pages/pages_network/login/login',
						});
						return false;
					}
				} else {
					uni.reLaunch({
						url: '/pages/pages_network/login/wx_login',
					});
				}
				return true;
			},
		});
	});
};
export default routeInterceptor;
